package com.wen.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.commonutils.GuLiUtils;
import com.wen.commonutils.ResultEntity;
import com.wen.commonutils.ordervo.OrderCourseInfoVO;
import com.wen.edu.api.OrderApi;
import com.wen.edu.entity.EduCourse;
import com.wen.edu.entity.vo.ChapterVO;
import com.wen.edu.entity.vo.front.FrontCourseInfoVO;
import com.wen.edu.entity.vo.front.FrontCourseQueryVO;
import com.wen.edu.service.EduChapterService;
import com.wen.edu.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author：Win
 * @Date: 2021/6/16 13:11
 * @Description: 前台课程页面显示功能接口
 */
@RestController
@RequestMapping("/edu/service/front/course")
public class FrontCourseController {
    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private EduChapterService eduChapterService;
    @Resource
    private OrderApi orderApi;

    /**
     * 带条件分页查询
     *
     * @param page 当前页
     * @param size 每页显示的数量
     * @return result of operation
     */
    @PostMapping("getFrontCourseList/{page}/{size}")
    public ResultEntity getFrontCourseList(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody FrontCourseQueryVO frontCourseQueryVO) {
        Page<EduCourse> pageCourse = new Page<>(page, size);
        Map<String, Object> map = eduCourseService.pageFrontCourse(pageCourse, frontCourseQueryVO);
        return ResultEntity.ok().data(map);
    }

    /**
     * 根据课程id查询课程信息(多表查询含课程大纲和小节)
     *
     * @param courseId 课程id
     * @return result of operation
     */
    @GetMapping("getFrontCourseInfo/{courseId}")
    public ResultEntity getFrontCourseInfo(@PathVariable("courseId") String courseId, HttpServletRequest request) {
//        查询课程基本信息(多表查询)
        FrontCourseInfoVO frontCourseInfoVO = eduCourseService.getFrontCourseInfo(courseId);
//        查询课程大纲(章节和小节)
        List<ChapterVO> list = eduChapterService.getChapterVideoByCourseId(courseId);
//        查询当前课程是否被当前用户购买
        boolean isBuyCourse = false;
        final String memberId = GuLiUtils.getMemberIdByJwtToken(request);
        if (!StringUtils.isEmpty(memberId))
            isBuyCourse = orderApi.isBuyCourse(memberId, courseId);
        return ResultEntity.ok().data("courseInfo", frontCourseInfoVO).data("list", list).data("isBuyCourse", isBuyCourse);
    }

    /**
     * 根据课程id查询课程信息(不含课程大纲和小节)，用于生成订单信息
     *
     * @param courseId 课程id
     * @return result of operation
     */
    @GetMapping("getOrderCourseInfo/{courseId}")
    public OrderCourseInfoVO getOrderCourseInfo(@PathVariable("courseId") String courseId) {
//        查询课程基本信息
        FrontCourseInfoVO frontCourseInfoVO = eduCourseService.getFrontCourseInfo(courseId);
        OrderCourseInfoVO orderCourseInfoVO = new OrderCourseInfoVO();
        BeanUtils.copyProperties(frontCourseInfoVO, orderCourseInfoVO);
//        返回生成订单时需要的课程信息
        return orderCourseInfoVO;
    }

    /**
     * 根据课程id更新课程信息,主要是用于课程购买成功后,修改课程的购买数
     *
     * @param courseId 课程id
     * @return result of operation
     */
    @GetMapping("updateCourseInfo/{courseId}")
    public boolean updateCourseInfo(@PathVariable("courseId") String courseId) {
//        查询课程基本信息
        EduCourse eduCourse = eduCourseService.getById(courseId);
        Long currentBuyCount = eduCourse.getBuyCount();
        eduCourse.setBuyCount(++currentBuyCount);
        return eduCourseService.updateById(eduCourse);
    }
}
